package com.baiyun.lovePush.controller;

import com.baiyun.lovePush.service.IPushService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;

/**
 * @author 派大星
 */
@RestController
public class PushController implements ApplicationContextAware {

    private static ApplicationContext context;
     @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
         context = applicationContext;
    }
    public static ApplicationContext getApplicationContext(){return context;}
    public static  Object getBean(String name){return  getApplicationContext().getBean(name);}
    public static  <T> T getBean(Class<T> clazz){
         return getApplicationContext().getBean(clazz);
    }


    /**
     * 定时任务设置发送时间 cron表达式 例子为早上  8点15分30秒
     */
    @Scheduled(cron = "30 15 8 * * ?")
    public void push() {
        try {
            IPushService iPushService =this.getBean(IPushService.class);
            iPushService.push();
            System.out.println("发送成功！");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}
